home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap16 / Grays2 / Grays2.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.5 KB  |  146 lines

  1. /*-----------------------------------------------
  2.    GRAYS2.C -- Gray Shades Using Palette Manager
  3.                (c) Charles Petzold, 1998
  4.   -----------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  9.  
  10. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  11.                     PSTR szCmdLine, int iCmdShow)
  12. {
  13.      static TCHAR szAppName[] = TEXT ("Grays2") ;
  14.      HWND         hwnd ;
  15.      MSG          msg ;
  16.      WNDCLASS     wndclass ;
  17.  
  18.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  19.      wndclass.lpfnWndProc   = WndProc ;
  20.      wndclass.cbClsExtra    = 0 ;
  21.      wndclass.cbWndExtra    = 0 ;
  22.      wndclass.hInstance     = hInstance ;
  23.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  24.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  25.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  26.      wndclass.lpszMenuName  = NULL ;
  27.      wndclass.lpszClassName = szAppName ;
  28.  
  29.      if (!RegisterClass (&wndclass))
  30.      {
  31.           MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
  32.                       szAppName, MB_ICONERROR) ;
  33.           return 0 ;
  34.      }
  35.      
  36.      hwnd = CreateWindow (szAppName, TEXT ("Shades of Gray #2"),
  37.                           WS_OVERLAPPEDWINDOW,
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,             
  40.                           NULL, NULL, hInstance, NULL) ;
  41.      
  42.      ShowWindow (hwnd, iCmdShow) ;
  43.      UpdateWindow (hwnd) ;
  44.      
  45.      while (GetMessage (&msg, NULL, 0, 0))
  46.      {
  47.           TranslateMessage (&msg) ;
  48.           DispatchMessage (&msg) ;
  49.      }
  50.      return msg.wParam ;
  51. }
  52.  
  53. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  54. {
  55.      static HPALETTE hPalette ;
  56.      static int      cxClient, cyClient ;
  57.      HBRUSH          hBrush ;
  58.      HDC             hdc ;
  59.      int             i ;
  60.      LOGPALETTE    * plp ;
  61.      PAINTSTRUCT     ps ;
  62.      RECT            rect ;
  63.      
  64.      switch (message)
  65.      {
  66.      case WM_CREATE:
  67.                // Set up a LOGPALETTE structure and create a palette
  68.  
  69.           plp = malloc (sizeof (LOGPALETTE) + 64 * sizeof (PALETTEENTRY)) ;
  70.  
  71.           plp->palVersion    = 0x0300 ;
  72.           plp->palNumEntries = 65 ;
  73.  
  74.           for (i = 0 ; i < 65 ; i++)
  75.           {
  76.                plp->palPalEntry[i].peRed   = (BYTE) min (255, 4 * i) ;
  77.                plp->palPalEntry[i].peGreen = (BYTE) min (255, 4 * i) ;
  78.                plp->palPalEntry[i].peBlue  = (BYTE) min (255, 4 * i) ;
  79.                plp->palPalEntry[i].peFlags = 0 ;
  80.           }
  81.           hPalette = CreatePalette (plp) ;
  82.           free (plp) ;
  83.           return 0 ;
  84.  
  85.      case WM_SIZE:
  86.           cxClient = LOWORD (lParam) ;
  87.           cyClient = HIWORD (lParam) ;
  88.           return 0 ;
  89.           
  90.      case WM_PAINT:
  91.           hdc = BeginPaint (hwnd, &ps) ;
  92.  
  93.                // Select and realize the palette in the device context
  94.  
  95.           SelectPalette (hdc, hPalette, FALSE) ;
  96.           RealizePalette (hdc) ;
  97.  
  98.                // Draw the fountain of grays
  99.  
  100.           for (i = 0 ; i < 65 ; i++)
  101.           {
  102.                rect.left   = i * cxClient / 64 ;
  103.                rect.top    = 0 ;
  104.                rect.right  = (i + 1) * cxClient / 64 ;
  105.                rect.bottom = cyClient ;
  106.  
  107.                hBrush = CreateSolidBrush (PALETTERGB (min (255, 4 * i), 
  108.                                                       min (255, 4 * i), 
  109.                                                       min (255, 4 * i))) ;
  110.                FillRect (hdc, &rect, hBrush) ;
  111.                DeleteObject (hBrush) ;
  112.           }
  113.           EndPaint (hwnd, &ps) ;
  114.           return 0 ;
  115.  
  116.      case WM_QUERYNEWPALETTE:
  117.           if (!hPalette)
  118.                return FALSE ;
  119.  
  120.           hdc = GetDC (hwnd) ;
  121.           SelectPalette (hdc, hPalette, FALSE) ;
  122.           RealizePalette (hdc) ;
  123.           InvalidateRect (hwnd, NULL, TRUE) ;
  124.  
  125.           ReleaseDC (hwnd, hdc) ;
  126.           return TRUE ;
  127.  
  128.      case WM_PALETTECHANGED:
  129.           if (!hPalette || (HWND) wParam == hwnd)
  130.                break ;
  131.  
  132.           hdc = GetDC (hwnd) ;
  133.           SelectPalette (hdc, hPalette, FALSE) ;
  134.           RealizePalette (hdc) ;
  135.           UpdateColors (hdc) ;
  136.  
  137.           ReleaseDC (hwnd, hdc) ;
  138.           break ;
  139.  
  140.      case WM_DESTROY:
  141.           DeleteObject (hPalette) ;
  142.           PostQuitMessage (0) ;
  143.           return 0 ;
  144.      }
  145.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  146. }